<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="box" style="display:flex; text-align:center">

    </div>
    <button class="prev">上一页</button>
    <button class="next">下一页</button>
</body>

</html>
<script>
    var paeg = 1;
    // 将对象格式转为字符串格式
    function objString(obj) {
        var str = '';
        // 遍历对象，将对象obj的属性拼接成key=value&的格式
        for (var key in obj) {
            str += `${key}=${obj[key]}&`
        }
        return str.slice(0, str, length - 1)
    }

    function ajax(method, url, params, cb) {
        // 创建xhr对象
        var xhr = new XMLHttpRequest();
        // 配置请求信息
        xhr.open(method, url + "?" + objString(params));
        // s设置回调函数
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                cb(JSON.parse(xhr.responseText))
            }
        }
        xhr.send()
    }

    document.querySelector(".prev").onclick = function () {
        if (paeg > 1) {
            ajax('get', `https://yantianfeng.com/api/emotion`, { paeg: --paeg, couunt: 10 }, function (data) {
                var str = data.emotions.map((item, index) => {
                    return `<div> <img src="${item.url}" /> <div> ${item.title} </div> </div>`
                }).join('')
                // 渲染表情
                document.querySelector('.box').innerHTML = str;
            })
        }
    }

    document.querySelector(".next").onclick = function () {
        if (paeg < 4) {
            ajax('get', `https://yantianfeng.com/api/emotion`, { paeg: ++paeg, couunt: 10 }, function (data) {
                var str = data.emotions.map((item, index) => {
                    return `<div> <img src="${item.url}" /> <div> ${item.title} </div> </div>`
                }).join('')
                // 渲染表情
                document.querySelector('.box').innerHTML = str;
            })
        }
    }
</script>